package services

import (
	"fmt"
	"strings"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/utils"
)

func getTableData(typeid int) []*models.Options {
	var param models.OptionsQueryParam
	param.Status = "1"
	param.Typeid = fmt.Sprintf("%d", typeid)
	opt := models.OptionsList(&param)
	return opt
}

//获取指定类型的配置信息
func GetOptionsForCache(typeid int) (option []*models.Options) {
	cacheKey := fmt.Sprintf("wxb_options_%d", typeid);
	if err := utils.GetCache(cacheKey, &option); err == nil {
		return option
	}
	option = getTableData(typeid)

	if err := utils.SetCache(cacheKey, option, 50000); err != nil {
		return nil
	}
	return option
}

//获取指定类型的配置信息[无序Map]
func GetOptionsForCacheMap(typeid int) map[string]map[string]string {

	cacheKey := fmt.Sprintf("wxb_options_one_map_%d", typeid);
	var option map[string]map[string]string
	option = make(map[string]map[string]string)

	if err := utils.GetCache(cacheKey, &option); err == nil {
		return option
	}

	opt := getTableData(typeid)

	if len(opt) > 0 {
		for _, value := range opt {
			m, _ := splitOptionContent(value.OptionContent)
			option[value.OptionKey] = m
		}
	}
	if err := utils.SetCache(cacheKey, option, 50000); err != nil {
		return nil
	}

	return option
}

//获取指定类型的配置信息[有序Slice]
func GetOptionsForCacheSlice(typeid int) map[string][]map[string]string {

	cacheKey := fmt.Sprintf("wxb_options_one_slice_%d", typeid);
	option := make(map[string][]map[string]string, 0)

	if err := utils.GetCache(cacheKey, &option); err == nil {
		return option
	}
	opt := getTableData(typeid)
	if len(opt) > 0 {
		for _, value := range opt {
			_, c := splitOptionContent(value.OptionContent)
			option[value.OptionKey] = c
		}
	}
	if err := utils.SetCache(cacheKey, option, 50000); err != nil {
		return nil
	}

	return option
}

//获得产品属性
func GetAttributeForCacheSlice() map[string][]map[string]string {

	cacheKey := "wxb_attribute_one_slice";
	attribute := make(map[string][]map[string]string, 0)

	if err := utils.GetCache(cacheKey, &attribute); err == nil {
		return attribute
	}
	opt := models.ErpProductsAttributeList(&models.ErpProductsAttributeParam{})
	if len(opt) > 0 {
		for _, value := range opt {
			_, c := splitOptionContent(value.OptionContent)
			attribute[value.OptionKey] = c
		}
	}
	if err := utils.SetCache(cacheKey, attribute, 50000); err != nil {
		return nil
	}

	return attribute
}

func splitOptionContent(con string) (map[string]string, []map[string]string) {

	if len(con) <= 0 {
		return nil, nil
	}
	contOpt := make(map[string]string)
	slc := make([]map[string]string, 0)

	if split := strings.Split(con, "\r\n"); len(split) > 0 {
		var i int
		for _, value := range split {
			sp := strings.Split(value, "|")
			if len(sp) == 2 {
				contOpt[sp[0]] = sp[1]
				slc = append(slc, map[string]string{"key": sp[0], "val": sp[0]})
			}else {
				i++
				str_i := fmt.Sprintf("%d", i)
				contOpt[str_i] = sp[0]
				slc = append(slc, map[string]string{"key": str_i, "val": sp[0]})
			}
		}
	}

	return contOpt, slc
}

func snakeString(s string) string {
	data := make([]byte, 0, len(s)*2)
	j := false
	num := len(s)
	for i := 0; i < num; i++ {
		d := s[i]
		if i > 0 && d >= 'A' && d <= 'Z' && j {
			data = append(data, '_')
		}
		if d != '_' {
			j = true
		}
		data = append(data, d)
	}
	return strings.ToLower(string(data[:]))
}
